<?php
namespace app\controller\model;

use app\BaseController;
use think\facade\Db;

class IncludeModel extends BaseController
{
    protected $tables;
    public function __construct(){
        $this->tables=new Tables();
    }

    public function qrcode($id=0,$username='',$userCode=''){
        include "phpqrcode.php";
        $url = Config::wxConfig()['htmlURL'];
        $value = $url."/#/pages/login/login?userCode=$userCode"; //二维码内容
        $errorCorrectionLevel = 'L';//容错级别
        $matrixPointSize = 6;//生成图片大小
        //生成二维码图片
        $imgName = './mycode/'."code_".$id.'.png';
        if(file_exists($imgName)){
            echojson(['myQRcode'=>apiURLHTTP().apiURL()."/".$imgName]);
        }
        \QRcode::png($value, $imgName, $errorCorrectionLevel, $matrixPointSize, 2);
        imagecreatefromstring(file_get_contents($imgName));
        if(file_exists($imgName)){
            $this->BGtoQRcode($imgName,$userCode);
            echojson(['myQRcode'=>apiURLHTTP().apiURL()."/".$imgName]);
        }else{
            echojson([],1,'生成失败');
        }
    }

    protected function BGtoQRcode($QRimg,$username){
        //给二维码添加背景图__FILE__
        $logo = $QRimg;//准备好二维码
        $saveQR = './qrcode.jpg';//已经背景图
        if(!file_exists($saveQR)){
            echojson([],1,'背景图不存在：'.$saveQR);
        }
        if(!file_exists($logo)){
            echojson([],1,'二维码不存在'.$logo);
        }
        if ($logo !== FALSE) {
            $QR = imagecreatefromstring(file_get_contents($saveQR));
            $logo = imagecreatefromstring(file_get_contents($logo));
            $QR_width = imagesx($QR);//二维码图片宽度
            $QR_height = imagesy($QR);//二维码图片高度
            $logo_width = imagesx($logo);//logo图片宽度
            $logo_height = imagesy($logo);//logo图片高度
            $logo_qr_width = $QR_width / 5;
            $scale = $logo_width/$logo_qr_width;
            $logo_qr_height = $logo_height/$scale;
            $from_width = ($QR_width - $logo_qr_width) / 2;
            //重新组合图片并调整大小
            imagecopyresampled($QR, $logo, 240, 937, 0, 0, 270, 230, $logo_width, $logo_height);
            imagepng($QR, $QRimg);

            //创建图片的实例
            $dst = imagecreatefromstring(file_get_contents($QRimg));
            //打上文字
            $fontColor = imagecolorallocate($dst, 255, 255, 255);//字体颜色
            $size=24;
            $txt=$username;
            $fontPath="./simkai.ttf";
            $fontBox = imagettfbbox($size, 0, $fontPath, $txt);//文字水平居中实质
            imagettftext($dst, $size, 0, ceil((900 - $fontBox[2])/2 ), 1233, $fontColor, $fontPath, $txt);
            //输出图片
            imagepng($dst, $QRimg);
            return true;
        }else{
            var_dump("12");
            return false;
        }
//输出图片

    }
}
?>